/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slicktest.factories;

import java.util.HashMap;
import java.util.Map;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

/**
 * used to hand out images without duplicating one that already exists.
 * @author enrightt
 */
public class ImageFactory {

    private static ImageFactory instance = new ImageFactory();
    //associate a path with a slick2d Image so we don't make duplicates
    private Map<String, Image> knownImages;

    private ImageFactory() {
        knownImages = new HashMap<String, Image>();
    }

    public static ImageFactory getInstance() {
        return instance;
    }

    public Image getImage(String path) throws SlickException {
        Image result = null;

        if (knownImages.containsKey(path)) {
            System.out.println(path + " is a known image path.");
            result = knownImages.get(path);
        } else {
            result = new Image(path);
            knownImages.put(path, result);
            System.out.println("Added " + path + " to known images.");
        }

        return result;
    }
}
